package com.lccviolet;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * @author LiChaochao
 * @Date 2021/9/24 19:08
 * @Description 引用队列
 */
public class ReferenceQueueDemo {
    public static void main(String[] args) {
        Object obj1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
        WeakReference<Object> weakReference = new WeakReference<>(obj1, referenceQueue);

        System.out.println("obj1 = " + obj1);
        System.out.println("weakReference = " + weakReference.get());
        System.out.println("referenceQueue = " + referenceQueue.poll());

        System.out.println("=================================");
        obj1 = null;
        System.gc();
        System.out.println("obj1 = " + obj1);
        System.out.println("weakReference = " + weakReference.get());
        System.out.println("referenceQueue = " + referenceQueue.poll());
    }
}
